﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.AllocationCommand;
using W.Warehouse.Domain.AllocationProduct;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Repository;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.AllocationCommandHadler
{
    public class AllocationCreateCommandHandler : IRequestHandler<AllocationCreateCommand, int>
    {
        private readonly IAllocationRepository _iallocationRepository;
        private readonly IAllocationProductRepository _iallocationProductRepository;
        public AllocationCreateCommandHandler(IAllocationRepository iallocationRepository, IAllocationProductRepository iallocationProductRepository)
        {
            _iallocationRepository = iallocationRepository;
            _iallocationProductRepository = iallocationProductRepository;
        }

        public async Task<int> Handle(AllocationCreateCommand request, CancellationToken cancellationToken)
        {
            
            using (var tran = _iallocationRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    #region 调拨信息表添加数据
                    Allocation allocation = new Allocation();
                    allocation.AllocationId = request.AllocationId;
                    allocation.AllocationNo = request.AllocationNo;
                    allocation.WarehouseId = request.WarehouseId;
                    allocation.AWarehouseName = request.AWarehouseName;
                    allocation.AssociateNumber = request.AssociateNumber;
                    allocation.SingleMaker = request.SingleMaker;
                    allocation.SingleTime = request.SingleTime;
                    allocation.AllocationRemarks = request.AllocationRemarks;
                    allocation.AllocationStatus = AuditStatus.等待审核;
                    allocation.ExamineReason = request.ExamineReason;
                    allocation.ExaminePeople = request.ExaminePeople;
                    allocation.ExamineTime = request.ExamineTime;
                    allocation.officeId = request.officeId;
                    allocation.userid = request.userid;
                    allocation.CreateName = "张三";
                    allocation.CreateTime = DateTime.Now;
                    allocation.IsDelete = false;
                    await _iallocationRepository.AddAsync(allocation);
                    #endregion

                    #region 调拨产品信息表添加数据
                    foreach (var item in request.AllocationProdutesList)
                    {
                        AllocationProductInfo allocatProduct = new AllocationProductInfo();
                        allocatProduct.AllocationProductId = item.AllocationProductId;
                        allocatProduct.ProductName = item.ProductName;
                        allocatProduct.ProductNo = item.ProductNo;
                        allocatProduct.ProductSpec = item.ProductSpec;
                        allocatProduct.ProductBatch = item.ProductBatch;
                        allocatProduct.ProductPrice = item.ProductPrice;
                        allocatProduct.Quantity = item.Quantity;
                        allocatProduct.BinLocationName = item.BinLocationName;
                        allocatProduct.WarehouseId = allocation.WarehouseId;
                        allocatProduct.officeId = item.officeId;
                        allocatProduct.userid = item.userid;
                        allocatProduct.AllocationId = allocation.AllocationId;
                        allocatProduct.CreateName = "张三";
                        allocatProduct.CreateTime = DateTime.Now;
                        allocatProduct.IsDelete = false;
                        await _iallocationProductRepository.AddAsync(allocatProduct);

                    }
                    #endregion
                    var code=await _iallocationRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    return code;    
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }


    }
}
